package com.sdk.es.http.gen;

import cn.hutool.core.util.StrUtil;
import com.sdk.es.sdk.constants.IndexConts;

import java.util.List;
import java.util.TreeMap;

/**
 * @author limaosheng
 * @create 2024-03-15 15:35
 * @desc 适用新版本
 */
public class GenEntityNew {
    //是否覆盖原文件
    public static final Boolean isOver = true;

    //需要使用Integer的字段,其它所有es内number类型字段默认使用Long
    public static final List<String> INTEGER_FILER = List.of(
            "communityId");

    public static void main(String[] args) {
        String index = IndexConts.TEST1;
        /**
         * 谷歌浏览器打开：kibana-Management-数据试图-创建索引，在页面选中先点击字段类型筛选：long、date、text,选择页面的类型和名称复制
         */
        //生成entity
        genEntity(index, STR1);
    }
    public static final String STR1 = "areaId\n" +
            "text\n" +
            "编辑\n" +
            "areaName\n" +
            "text\n" +
            "编辑\n" +
            "buildId\n" +
            "text\n" +
            "编辑\n" +
            "building\n" +
            "text\n" +
            "编辑\n" +
            "cardNo\n" +
            "text\n" +
            "编辑\n" +
            "caringType\n" +
            "text\n" +
            "编辑\n" +
            "city\n" +
            "text\n" +
            "编辑\n" +
            "communityId\n" +
            "long\n" +
            "编辑\n" +
            "communityName\n" +
            "text\n" +
            "编辑\n" +
            "confId\n" +
            "long\n" +
            "编辑\n" +
            "createTime \n" +
            "date\n" +
            "编辑\n" +
            "createTimeStr\n" +
            "text\n" +
            "编辑\n" +
            "deviceId\n" +
            "long\n" +
            "编辑\n" +
            "deviceMainGate\n" +
            "long\n" +
            "编辑\n" +
            "deviceModelLocatX\n" +
            "text\n" +
            "编辑\n" +
            "deviceModelLocatY\n" +
            "text\n" +
            "编辑\n" +
            "deviceName\n" +
            "text\n" +
            "编辑\n" +
            "deviceSource\n" +
            "long\n" +
            "编辑\n" +
            "deviceType\n" +
            "long\n" +
            "编辑\n" +
            "faceImageId\n" +
            "text\n" +
            "编辑\n" +
            "gmtCreate\n" +
            "text\n" +
            "编辑\n" +
            "govBasicId\n" +
            "long\n" +
            "编辑\n" +
            "govBasicName\n" +
            "text\n" +
            "编辑\n" +
            "govCommunityId\n" +
            "long\n" +
            "编辑\n" +
            "gridPerson\n" +
            "text\n" +
            "编辑\n" +
            "gridPersonId\n" +
            "long\n" +
            "编辑\n" +
            "gridTel\n" +
            "text\n" +
            "编辑\n" +
            "houseId\n" +
            "long\n" +
            "编辑\n" +
            "inOutFlag\n" +
            "long\n" +
            "编辑\n" +
            "isNormalTemperature\n" +
            "long\n" +
            "编辑\n" +
            "labelId\n" +
            "long\n" +
            "编辑\n" +
            "labelName\n" +
            "text\n" +
            "编辑\n" +
            "month\n" +
            "long\n" +
            "编辑\n" +
            "monthDay\n" +
            "text\n" +
            "编辑\n" +
            "name\n" +
            "text\n" +
            "编辑\n" +
            "paasId\n" +
            "long\n" +
            "编辑\n" +
            "passKind\n" +
            "text\n" +
            "编辑\n" +
            "picUrl\n" +
            "text\n" +
            "编辑\n" +
            "picture1\n" +
            "text\n" +
            "编辑\n" +
            "province\n" +
            "text\n" +
            "编辑\n" +
            "rentType\n" +
            "long\n" +
            "编辑\n" +
            "roomNum\n" +
            "text\n" +
            "编辑\n" +
            "ruleId\n" +
            "long\n" +
            "编辑\n" +
            "ruleName\n" +
            "text\n" +
            "编辑\n" +
            "serial\n" +
            "text\n" +
            "编辑\n" +
            "sex\n" +
            "long\n" +
            "编辑\n" +
            "similarity\n" +
            "text\n" +
            "编辑\n" +
            "taskId\n" +
            "long\n" +
            "编辑\n" +
            "tel\n" +
            "text\n" +
            "编辑\n" +
            "temperature\n" +
            "text\n" +
            "编辑\n" +
            "tenementId\n" +
            "long\n" +
            "编辑\n" +
            "thirdDeviceId\n" +
            "text\n" +
            "编辑\n" +
            "type\n" +
            "long\n" +
            "编辑\n" +
            "unitId\n" +
            "long\n" +
            "编辑\n" +
            "unitName\n" +
            "text\n" +
            "编辑\n" +
            "userId\n" +
            "long\n" +
            "编辑\n" +
            "warnTime\n" +
            "date\n" +
            "编辑\n" +
            "warnType\n" +
            "text\n" +
            "编辑\n" +
            "year\n" +
            "long";
    /**
     * 生成entiy和entityQuery
     *
     * @param index
     * @param str
     */
    public static void genEntity(String index, String str) {
        String[] arr1 = str.split("\n编辑\n");
        TreeMap<String, String> map = new TreeMap<>();
        for (int i = 0; i < arr1.length; i++) {
            String[] oneArr = arr1[i].split("\n");
            map.put(oneArr[0],oneArr[1]);
        }
        /**
         * entity
         */
        StringBuffer sb = new StringBuffer();
        sb.append("package com.digitallife.community.elasticsearch.gen.entity;\n" +
                "\n" +
                "import com.digitallife.community.saas.es.constants.IndexConts;\n" +
                "import com.digitallife.community.saas.es.entity.common.EsBaseEntity;\n" +
                "import lombok.Data;\n" +
                "import org.springframework.data.elasticsearch.annotations.Document;\n" +
                "\n" +
                "@Document(indexName = IndexConts."+index.toUpperCase()+" + \"_#{@indexNameGenerator.getYearMonthStr()}\")\n" +
                "@Data\n" +
                "public class "+ StrUtil.upperFirst(StrUtil.toCamelCase(index))+" extends EsBaseEntity {\n"
        );
        for(String key : map.keySet()){
            sb.append("    //\r\n");
            String javaType = esTypeToJavaType(map.get(key),key);
            sb.append("\tprivate " + javaType + " " + key + ";\r\n");
        }
        sb.append("}");
        String path = System.getProperty("user.dir") + "\\common\\src\\main\\java\\com\\digitallife\\community\\elasticsearch\\gen\\entity\\"
                + StrUtil.upperFirst(StrUtil.toCamelCase(index)) + ".java";
        FileUtil.write(path, sb.toString(),isOver);
    }
    private static String esTypeToJavaType(String esType,String key) {
        String javaType = "String";
        if ("long".equals(esType)) {
            javaType = "Long";
        }else if("text".equals(esType)){
            javaType = "String";
        }else if("date".equals(esType)){
            javaType = "LocalDateTime";
        }
        if(INTEGER_FILER.contains(key)){
            javaType = "Integer";
        }
        return javaType;
    }
}
